package com.changgou.goods.controller;

import com.changgou.goods.pojo.Spec;
import com.changgou.goods.service.SpecService;
import com.github.pagehelper.PageInfo;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/spec")
@CrossOrigin
public class SpecController {

    @Autowired
    private SpecService specService;

    /****
     * 查找所有规格列表
     * @return
     */
    @GetMapping
    public Result<List<Spec>> findAll() {
        List<Spec> specs = specService.findAll();
        return new Result<>(true, StatusCode.OK, "查询成功", specs);
    }

    /****
     * 添加规格
     * @param spec
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Spec spec) {
        specService.add(spec);
        return new Result<>(true, StatusCode.OK, "添加成功");
    }

    /****
     * 条件查询所有
     * @param spec
     * @return
     */
    @PostMapping("/search")
    public Result<List<Spec>> findList(@RequestBody(required = false) Spec spec) {
        List<Spec> specs = specService.findList(spec);
        return new Result<>(true, StatusCode.OK, "查询成功", specs);
    }

    /****
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/search/{page}/{size}")
    public Result<PageInfo> findPage(@PathVariable Integer page, @PathVariable Integer size) {
        PageInfo<Spec> pageInfo = specService.findPage(page, size);
        return new Result<>(true, StatusCode.OK, "查询成功", pageInfo);
    }

    /****
     * 分页条件查询
     * @param page
     * @param size
     * @param spec
     * @return
     */
    @PostMapping("/search/{page}/{size}")
    public Result<PageInfo> findPage(@PathVariable Integer page, @PathVariable Integer size,
                                     @RequestBody(required = false) Spec spec) {
        PageInfo<Spec> pageInfo = specService.findPage(page, size, spec);
        return new Result<>(true, StatusCode.OK, "查询成功", pageInfo);
    }

    /****
     * 根据Id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Spec> findById(@PathVariable Integer id) {
        Spec spec = specService.findById(id);
        return new Result<>(true, StatusCode.OK, "查询成功", spec);
    }

    /****
     * 根据Id修改
     * @param id
     * @param spec
     * @return
     */
    @PutMapping("/{id}")
    public Result update(@PathVariable Integer id, @RequestBody Spec spec) {
        spec.setId(id);
        specService.update(spec);
        return new Result<>(true, StatusCode.OK, "修改成功");
    }

    /****
     * 根据id删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        specService.delete(id);
        return new Result<>(true, StatusCode.OK, "删除成功");
    }

    /***
     * 根据分类ID查询对应的规格列表
     */
    @GetMapping(value = "/category/{id}")
    public Result<List<Spec>> findByCategoryId(@PathVariable(value = "id") Integer id) {
        //调用Service查询
        List<Spec> specs = specService.findByCategoryId(id);
        return new Result<List<Spec>>(true, StatusCode.OK, "查询成功", specs);
    }
}
